/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   TollWayServiceImpl.java for com.pengtu.gsj.service.impl.check.zhgl.industrymanage    </p>
 * 
 * <p>最后修改 : $: 2017年8月21日-上午10:09:33 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.zhgl.industrymanage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.zhgl.industrymanage.TollWayService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * TollWayServiceImpl
 * 
 * 2017年8月21日 上午10:09:33
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class TollWayServiceImpl implements TollWayService {
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Override
	public Map<String, Object> getTollWayMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		String whetherCancel = (String)jsonObject.get("whetherCancel");
		List<List<String>> attmentList = new ArrayList<List<String>>();
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取检查通报文件
		List<Attachment> jctbList = systemManageService.getCheckAttachmentByTag(startTime, String.valueOf(Integer.valueOf(endTime)-1), "sfgljg_jctb");
		//获取整改意见文件
		List<Attachment> zgyjList = systemManageService.getCheckAttachmentByTag(startTime, String.valueOf(Integer.valueOf(endTime)-1), "sfgljg_zgyj");
		//获取落实情况文件
		List<Attachment> lsqkList = systemManageService.getCheckAttachmentByTag(startTime, String.valueOf(Integer.valueOf(endTime)-1), "sfgljg_lsqk");
		
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		double total =0; 				//检查得分
		double percentage =0; 			//系统完善度
		double tableFull  = yearFull * years.length;	//满分
		//是否省级区域内普通干线公路已全部取消经营性收费公路 ：1是，0否
		if (whetherCancel.equals("1")) {
			total = tableFull;
		} else {
			for ( int i = 0; i<years.length; i++){
				List<String> checkList = Arrays.asList("0", "0", "0", "0", "0");
				checkList.set(0, String.valueOf(years[i]));
				int temp = 0;
				//判断每年是否有管理目标文件
				if (jctbList.size() > 0) {
					for (int j = 0; j< jctbList.size(); j++) {
						if (years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", jctbList.get(j).getFileTime()))) {
							checkList.set(1, "1");
							temp ++ ;
							break;
						}
					}
				}
				
				if (zgyjList.size() > 0) {
					for (int j = 0; j< zgyjList.size(); j++) {
						if (years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", zgyjList.get(j).getFileTime()))) {
							checkList.set(2, "1");
							temp ++ ;
							break;
						} 
					}
				}
				
				if (lsqkList.size() > 0) {
					for (int j = 0; j< lsqkList.size(); j++) {
						if (years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", lsqkList.get(j).getFileTime()))) {
							checkList.set(3, "1");
							temp ++ ;
							break;
						}
					}
				}
				//年度得分
				double remark = 0;
				if (temp >= 3) {
					total += yearFull;
					remark += yearFull;
				} else if (temp == 2) {
					total += yearFull - yearPart;
					remark += yearFull - yearPart;
				} else if (temp == 1) {
					total += yearFull - yearPart *2;
					remark += yearFull - yearPart *2;
				}
					
				if (remark < 0) {
					total = 0;
					remark = 0;
				}
				checkList.set(4, String.valueOf(MathsUtils.formatDouble(remark, 2)));
				attmentList.add(checkList);
			}
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(MathsUtils.formatDouble(total,2) *100 , tableFull, 2) ;
		map.put("percent", percentage);
		map.put("years", years);
		map.put("whetherCancel", whetherCancel);
		map.put("attmentList", attmentList);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
